package com.junxonline.plat.task;

import com.junxonline.common.util.HttpUtils;
import com.junxonline.plat.dao.repository.DatabaseRepository;
import com.junxonline.plat.dao.vo.HouseholdVO;
import com.junxonline.plat.service.charge.ChargeService;
import com.junxonline.plat.service.database.DatabaseService;
import org.apache.http.HttpResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收费模块定时任务
 */
@Component
public class ChargeTask {

    @Value("${wms.aliyun.sms.host}")
    private String host;

    @Value("${wms.aliyun.sms.path}")
    private String path;

    @Value("${wms.aliyun.sms.code}")
    private String appCode;

    @Value("${wms.aliyun.sms.template.id}")
    private String templateId;

    @Autowired
    private ChargeService chargeService;

    @Autowired
    private DatabaseService databaseService;

    private Logger logger = LogManager.getLogger();

    /**
     * 每个月的一号的0点0分0秒开始执行定时任务
     */
//    @Scheduled(cron = "0 0 0 1 * ?")
//    public void checkHouseholdChargeAndSendMessage() {
//
//        // 查询需要发送短信的住户数据
//        List<HouseholdVO> households = chargeService.findByNeedChargeHousehold();
//
//        // 遍历集合
//        for (HouseholdVO householdVO : households) {
//
//            // 调用阿里大于开始给住户发短信
//            logger.info("发送缴费通知短信给[" + householdVO.getHouseholdNo() + "]" +
//                    householdVO.getHouseholdName() + " 手机号为[" +
//                    householdVO.getHouseholdPhone() + "] 费用为[" +
//                    householdVO.getChargeMoney() + "]元");
//            // 发送短信
//            try {
//
//                String method = "POST";
//                Map<String, String> headers = new HashMap<>();
//                headers.put("Authorization", "APPCODE " + appCode);
//                Map<String, String> querys = new HashMap<>();
//                querys.put("phone", householdVO.getHouseholdPhone());
//                querys.put("templateId", templateId);
//                querys.put("variable", "money:" + householdVO.getChargeMoney());
//                Map<String, String> bodys = new HashMap<>();
//
//                HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//                System.out.println(response.toString());
//
//                logger.info("发送成功...");
//            } catch (Exception e) {
//                logger.info("发送失败...");
//            }
//        }
//
//    }

    /**
     * 每天凌晨两点执行一次
     */
    @Scheduled(cron = " 0 0 2 * * ?")
    public void restoreDatabase() {

        logger.info("每天还原数据库定时任务启动...");
        databaseService.databaseRestore();

    }

    /**
     * 每隔一分钟执行一次
     */
    @Scheduled(cron = "0 0 0 1 * ?")
    public void testTask() {

        logger.info("每分钟测试定时任务启动...");
//        checkHouseholdChargeAndSendMessage();

    }

}
